﻿using System.Web;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;

namespace GroupChallenge.Web.Services
{
    [EnableClientAccess()]
    public class GroupChallengeDomainService : DomainService
    {
        private GroupChallenge _ClientContext = new GroupChallenge();

        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the Get query.

        private GameManager Context
        {
            get
            {
                GameManager ret = HttpContext.Current.Application["CurrentGameManager"] as GameManager;

                if (null == ret)
                {
                    ret = new GameManager();

                    // Should be in the application cache.
                    HttpContext.Current.Application["CurrentGameManager"] = ret;
                }

                return ret;
            }
        }

        public Game GetCurrentGame()
        {
            return Context.CurrentGame;
        }

        public void StartGame(Game game)
        {
            Context.ResetGame();
        }

        public FriendlyResult StopGame()
        {
            return Context.StopGame();
        }

        public FriendlyResult ResetGame()
        {
            return Context.ResetGame();
        }

        public IQueryable<PlayerImage> GetPlayerImageList()
        {
            return Context.GetPlayerImageList().AsQueryable();
        }

        public IQueryable<Player> GetPlayers()
        {
            return Context.GamePlayers().AsQueryable();
        }

        public void DeletePlayer(Player player)
        {
            Context.DeletePlayer(player);
        }

        public IQueryable<Question> GetQuestions()
        {
            return Context.Questions().AsQueryable();
        }

        // Can't use Delete or Remove or we get a DomainService error requiring a Domain object instead of this parameter.
        public void DropQuestion(int questionId)
        {
            Context.DeleteQuestion(questionId);
        }

        public FriendlyResult JoinGame(string nickName, string fullName, int playerId)
        {
            return Context.JoinGame(nickName, fullName, playerId);
        }

        public bool LeaveGame(string nickName)
        {
            return Context.LeaveGame(nickName);
        }

        public FriendlyResult SaveQuestions()
        {
            return Context.SaveQuestions();
        }

        public FriendlyResult SetCurrentQuestion(int questionId)
        {
            return Context.SetCurrentQuestion(questionId);
        }

        public FriendlyResult DeletePlayerImage(int playerId)
        {
            return Context.DeletePlayerImage(playerId);
        }
    }
}